package com.cosina.game.crystallight.dao;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.SharedPreferences;

import com.cosina.game.crystallight.CrystalLightActivity;
import com.cosina.game.crystallight.model.profile.Profile;
import com.cosina.game.crystallight.model.profile.ProfileManager;

public class ProfileDAOFacade {
	
	public static String SP_CRYSTALLIGHT = "SP_CRYSTALLIGHT";
	public static String NODE_PROFILESNAMES = "NODE_PROFILESNAMES";
	public static String NODE_CURRENTPROFILENAMEINDEX = "NODE_CURRENTPROFILENAMEINDEX";
	
	public static String NODE_SKILLS = "NODE_SKILLS";
	public static String NODE_SKILLPOINT = "NODE_SKILLPOINT";
	public static String NODE_OVEREDTOLLGATES = "NODE_OVEREDTOLLGATES";
	

	
	public static String[] getProfileNames(){
		SharedPreferences sp = get(SP_CRYSTALLIGHT);
		String value = sp.getString(NODE_PROFILESNAMES, "");
		if("".equals(value)){
			String[] result = new String[7];
			Arrays.fill(result, ProfileManager.EMPTY_PROFILE);
			return result;
		}
		return value.split(",");
	}
	
	
	public static int getCurrentProfileNameIndex(){
		SharedPreferences sp = get(SP_CRYSTALLIGHT);
		return sp.getInt(NODE_CURRENTPROFILENAMEINDEX,-1);
	}
	
	public static void saveCurrentProfileNameIndex(int index){
		get(SP_CRYSTALLIGHT).edit().putInt(NODE_CURRENTPROFILENAMEINDEX, index).commit();
	}
	
	
	public static String NODE_CAMPAIGN_SCORE = "NODE_CAMPAIGN_SCORE";
	public static String NODE_BATTLE_SCORE = "NODE_BATTLE_SCORE";
	public static String NODE_GAMES_PLAYED = "NODE_GAMES_PLAYED";
	public static String NODE_ENEIMES_KILLED = "NODE_ENEIMES_KILLED";
	public static String NODE_CRYSTAL_CREATED = "NODE_CRYSTAL_CREATED";
	public static String NODE_MINES_CREATED = "NODE_MINES_CREATED";
	public static String NODE_TOWERRS_BUILT = "NODE_TOWERRS_BUILT";
	
	
	public static Profile loadProfile(String name){
		SharedPreferences sp = get(name);
		String skillString = sp.getString(NODE_SKILLS, "0,0,0,0,0");
		int skillPoint = sp.getInt(NODE_SKILLPOINT, 0);
		String overedIdString = sp.getString(NODE_OVEREDTOLLGATES, "");
		String[] overedIds = new String[0];
		if(overedIdString.equals("") == false){
			overedIds = overedIdString.split(",");
		}
		
		int[] stats = new int[7];
		stats[0] = sp.getInt(NODE_CAMPAIGN_SCORE, 0);
		stats[1] = sp.getInt(NODE_BATTLE_SCORE, 0);
		stats[2] = sp.getInt(NODE_GAMES_PLAYED, 0);
		stats[3] = sp.getInt(NODE_ENEIMES_KILLED, 0);
		stats[4] = sp.getInt(NODE_CRYSTAL_CREATED, 0);
		stats[5] = sp.getInt(NODE_MINES_CREATED, 0);
		stats[6] = sp.getInt(NODE_TOWERRS_BUILT, 0);
		
		return new Profile(name,string2Array(skillString),skillPoint,overedIds,stats);
	}
	
	public static void saveCurrentProfile(){
		ProfileDAOFacade.saveProfile(ProfileManager.getCurrentProfile());
	}
	
	public static void saveProfile(Profile profile){
		SharedPreferences sp = get(profile.name);
		sp.edit().clear()
		.putString(NODE_SKILLS, array2String(profile.skillManager.getSkillLevels()))
		.putInt(NODE_SKILLPOINT, profile.skillManager.getSkillPoint())
		.putString(NODE_OVEREDTOLLGATES, profile.tollGateManager.getOveredPersistString())
		
		.putInt(NODE_CAMPAIGN_SCORE, profile.stats.data[0])
		.putInt(NODE_BATTLE_SCORE, profile.stats.data[1])
		.putInt(NODE_GAMES_PLAYED, profile.stats.data[2])
		.putInt(NODE_ENEIMES_KILLED, profile.stats.data[3])
		.putInt(NODE_CRYSTAL_CREATED, profile.stats.data[4])
		.putInt(NODE_MINES_CREATED, profile.stats.data[5])
		.putInt(NODE_TOWERRS_BUILT, profile.stats.data[6])
		
		.commit();
		;
	}
	
	private static String array2String(int[] array){
		String temp = Arrays.toString(array);
		return temp.substring(1, temp.length()-1);
	}
	
	
	private static int[] string2Array(String str){
		List<Integer> list = new ArrayList<Integer>();
		for(String each : str.split(",")){
			list.add(Integer.valueOf(each.trim()));
		}
		int[] result = new int[list.size()];
		for(int i=0; i<result.length; i++){
			result[i] = list.get(i);
		}
		return result;
	}
	
	public static SharedPreferences get(String pskey){
		return CrystalLightActivity.self.getSharedPreferences(pskey, Activity.MODE_WORLD_WRITEABLE);
	}

	public static void saveNewProfileName(String textValue, int index) {
		String[] names = getProfileNames();
		names[index] = textValue;
		saveProfileNames(names);
	}
	
	private static void saveProfileNames(String[] names){
		StringBuilder persistString = new StringBuilder();
		for(int i=0; i<7; i++){
			persistString.append(names[i]);
			if(i != 6){
				persistString.append(",");
			}
		}
		SharedPreferences sp = get(SP_CRYSTALLIGHT);
		sp.edit().putString(NODE_PROFILESNAMES, persistString.toString()).commit();
	}

	public static void removeProfileName(int i) {
		String[] names = getProfileNames();
		String name = names[i];
		names[i] = ProfileManager.EMPTY_PROFILE;
		saveProfileNames(names);
		
		removeProfileData(name);
		
		Profile profile = ProfileManager.getCurrentProfile();
		if(null != profile && profile.name.equals(name)){
			ProfileManager.removeCurrentProfile();
		}
	}
	
	public static void removeProfileData(String name){
		get(name).edit().clear().commit();
	}
}
